using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Diagnostics;

namespace LanguageWriterHelper
{
    [DebuggerStepThrough()]
    public abstract class VisitorBase
    {
        VisitorManager manager;
        //bool outputCData = false;
        Stack<string> names = new Stack<string>();

        
        public VisitorManager Manager
        {
            get { return manager; }
            set { manager = value; }
        }

        public void Write(string value)
        {
            manager.Write(value);
        }

        public void WriteIndent()
        {
            manager.Formatter.WriteIndent();
        }

        public void WriteOutdent()
        {
            manager.Formatter.WriteOutdent();
        }

        public void WriteStartElement(string name)
        {
            names.Push(name);

            Write("<" + name + ">");
            WriteIndent();
        }

        public void WriteEndElement()
        {
            string name = names.Pop();
        
            WriteOutdent();
            Write("</" + name + ">");
        }

        public void WriteEmptyElement(string name)
        {
            Write("<" + name + "/>");
        }

        public void WriteElement(string name, string value)
        {
            Write("<" + name + ">" + value + "</" + name + ">");
        }

        public string GetByteArrayText(byte[] value)
        {
            if ((value != null) && (value.Length != 0))
            {
                string[] parts = new string[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    parts[i] = value[i].ToString("x2", CultureInfo.InvariantCulture);
                }

                return string.Concat(parts);
            }

            return "null";
        }
    }
}
